﻿$(function () {
    
    /* Initial view setup */
    $("#PublishedOn").datetimepicker();
    CKEDITOR.replace('ShortText',   { height: 300   });
    CKEDITOR.replace('LongText',    { height: 800   });

    /* AjaxSubmit and preview button logic */
    var previewOn       = false,
        autoSaved       = false,
        articleForm     = $('.updateArticle form').addClass('ajaxForm');

    $('.actions-wrapper')
        .on('mouseup',  '.preview',  function () { previewOn = true;  })
        .on('mouseup',  '.submit',   function () { previewOn = false; });

    articleForm
        .ajaxForm({ 
            beforeSubmit: function(arr, $form, options) { 
                msacademic.transferCKTextToTextareas();
                msacademic.hideToastNotification();
            },

            success: function () { 
                if(previewOn)   { window.open($('.actions-wrapper .preview').data('url'), '_blank'); }

                if (!autoSaved) {
                    msacademic.displayToastNotification("Promjene na članku su spremljene!", '');
                } else {
                    msacademic.displayToastNotification("Dogodila se automatska pohrana stanja članka", '(svakih 5 minuta)', 15000);
                }
                
                msacademic.loading('off');
                autoSaved = false;
            }  
        });

    var autoSave = setInterval(function () {
        
        msacademic.loading('on');

        autoSaved = true;
        previewOn = false;
        msacademic.transferCKTextToTextareas();

        articleForm.submit();
    }, 300000);

});